<template>
  <div id="modal">
    <input type="text" :disabled="state.isLoading" v-model="url" id="url" placeholder="输入URL...">
    <div v-if="!state.isLoading">
      <button @click="handleAddItem" id="add-item">添加</button>
      <button @click="handleCancel" id="close-modal">取消</button>
    </div>
    <div v-else>
      <button id="add-item" :disabled="state.isLoading">加载中...</button>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useStore } from 'vuex'

export default {
  setup() {
    let url = ref('')
    let { ipcRenderer } = global.electron

    let { commit, state } = useStore()

    const handleCancel = () => {
      commit('setIsOpen', false)
    }

    const handleAddItem = () => {
      commit('setIsLoading', true)
      ipcRenderer.send('new-url', url)
    }

    return {
      handleCancel,
      handleAddItem,
      url,
      state
    }
  }
}
</script>

<style scoped>
#modal {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  align-content: center;
  flex-wrap: wrap;
}

#url {
  flex-grow: 1;
  width: 100%;
  margin: 0 25px 15px;
  padding: 10px;
}

#modal button {
  padding: 10px;
}

#close-modal {
  background: white;
  color: black;
  margin-left: 15px;
}

#add-item {
  margin-left: 25px;
}

.read-item {
  display: flex;
  align-items: center;
  align-content: center;
  border-bottom: lightgray 2px solid;
  background: #FAFAFA;
  padding: 10px;
}

.read-item img {
  width: 20%;
  margin-right: 25px;
}
</style>